import datetime
import subprocess

from django.db.models import Q
from django.http import JsonResponse

from lapi_app.models import App_result, App_script
from lapi_app.models.testcase_model.testcase import Testcase, case_result
from lapi_app.models.user_model.user_info import Userinfo


def get_week_date():
    today = datetime.datetime.now()
    date1 = today.date() + datetime.timedelta(days=-1)
    date2 = today.date() + datetime.timedelta(days=-2)
    date3 = today.date() + datetime.timedelta(days=-3)
    date4 = today.date() + datetime.timedelta(days=-4)
    date5 = today.date() + datetime.timedelta(days=-5)
    date6 = today.date() + datetime.timedelta(days=-6)
    date7 = today.date() + datetime.timedelta(days=-7)
    date = [date7, date6, date5, date4, date3, date2, date1]
    return date


def case_count(request):
    date = get_week_date()
    result = []
    for i in date:
        case = Testcase.objects.filter(create_time__gte=str(i) + ' 00:00:00',
                                       create_time__lte=str(i) + ' 23:59:59')
        case_total = len(case)
        result.append(case_total)
    return JsonResponse({
        "code": 200,
        "date": date,
        "content": result
    })


def user_case_count(request):
    users = Userinfo.objects.all().order_by("-id")
    user_list = []
    case_list = []
    for u in users:
        user_list.append(u.username)
        case_list.append(Testcase.objects.filter(t_user_id=u.id).count())
    return JsonResponse({
        "code": 200,
        "user_list": user_list,
        "case_list": case_list
    })


def api_run_count(request):
    cur_date = datetime.datetime.now().date()
    week = cur_date - datetime.timedelta(weeks=1)
    run_true = case_result.objects.filter(status=200, create_time__gte=week).count()
    run_fail = case_result.objects.filter(~Q(status=200), create_time__gte=week).count()
    mouth_add_count = Testcase.objects.filter(create_time__month=datetime.datetime.utcnow().month).count()
    all_count = Testcase.objects.all().count()
    return JsonResponse({
        "code": 200,
        "run_true": run_true,
        "run_fail": run_fail,
        "mouth_add_count": mouth_add_count,
        "all_case_count": all_count

    })


def app_ui_run_count(request):
    date = get_week_date()
    result_true = []
    result_false = []
    for i in date:
        true_total = App_result.objects.filter(create_time__gte=str(i) + ' 00:00:00',
                                               create_time__lte=str(i) + ' 23:59:59', result_type=1).count()
        false_total = App_result.objects.filter(create_time__gte=str(i) + ' 00:00:00',
                                                create_time__lte=str(i) + ' 23:59:59', result_type=0).count()
        result_true.append(true_total)
        result_false.append(false_total)
    return JsonResponse({
        "code": 200,
        "date": date,
        "result_true": result_true,
        "result_false": result_false
    })


def app_script_user(request):
    users = Userinfo.objects.all().order_by("id")
    user_list = []
    script_list = []
    for u in users:
        user_list.append(u.username)
        script_list.append(App_script.objects.filter(user_id=u.id).count())
    return JsonResponse({
        "code": 200,
        "user_list": user_list,
        "script_list": script_list
    })
